/**
 * 风险测评结果
 * Created by KY on 2017/8/10.
 */
define(['ldfw/core', 'art', 'text!tpl/risk_result.tpl', 'comps/session', 'comps/ajax', 'ldfw/ldlibs/dict'],
    function (LDFW, art, tpl, session, ajax, dict) {
    let rowTpl = `{{each $data data index}}
                <tr>
                    <td>{{data.min_score}}-{{data.max_score}}分</td>
                    <td>{{data.risk_accept_name}}</td>
                    <td>{{data.adapt_product}}</td>
                </tr>
                {{/each}}`;
    let View = LDFW.Container.extend({
        tpl: tpl,
        className: 'panel',
        events: {
            'click a[name="confirm"]':'confirm',
            'click a[name="reEvaluation"]':'reEvaluation',
            'change #risk_result_agree':'toggleConfirm'
        },
        initialize: function () {
            this.delay = 5
            this.on('shown',this.loadData)
        },
        setRiskResult(result){
            this.riskResult = result
        },
        confirm:function(event){
            let curr_cust = session.getCurrentUser(),
                _this = this
            if($(event.target).hasClass('weui-btn_disabled')){
                $.alert('您还未同意上述评测结果',function(){
                    _this.el.scrollTop = _this.el.scrollHeight - _this.el.clientHeight
                })
            }else{
                //已认证
                if(curr_cust.qual_auth===1){
                    $.when(ajax.post('cpliL.3.102',{mark_type:9,relation_field:_this.riskResult.answer_id}),
                        ajax.post('cpliL.3.107',{
                            answer_id:_this.riskResult.answer_id,
                            before_risk_accept:curr_cust.risk_accept,
                            after_risk_accept:_this.riskResult.risk_accept,
                            before_low_risk_flag:curr_cust.low_risk_flag,
                            low_risk_flag:_this.riskResult.low_risk_flag,
                            ques_invest_num:_this.riskResult.ques_invest_num,
                            cust_no:curr_cust.cust_no
                        })
                    ).done(function(){
                        session.flush()
                        LDFW.navigate('main')
                    })
                }else{
                    //提交认证审核和问卷结果确认
                    $.when(ajax.post('cpliL.3.102',{mark_type:9,relation_field:_this.riskResult.answer_id}),
                        ajax.post('cpliL.3.8',{
                            cust_type:curr_cust.cust_type,
                            audit_type:1,
                            answer_id:_this.riskResult.answer_id
                        })
                    ).done(function(){
                        session.flush()
                        LDFW.navigate('main')
                    })
                }
            }
        },
        reEvaluation:function(){
            LDFW.navigate('risk_assessment')
        },
        loadData: function () {
            const _this = this
            let curr_cust = session.getCurrentUser()
            //查询最后的问卷结果
            ajax.post('cpliL.1.58', {cust_type: curr_cust.cust_type}).success(function (resp) {
                let result = resp.result
                if(result){
                    let risk_accept = '',
                        adapt_product = _this.getAdaptProduct(result.risk_accept)
                    if(curr_cust.cust_type===1){
                        risk_accept = dict.getDict('risk_accept_co', result.risk_accept)
                    }else if(curr_cust.cust_type===2){
                        risk_accept = dict.getDict('risk_accept_cust', result.risk_accept)
                    }
                    _this.model.set('risk_accept',risk_accept)
                    _this.model.set('adapt_product',adapt_product)
                    _this.model.set('risk_result',result.ques_invest_num+'分~')
                    _this.model.set('pd_kind_perfer',result.pd_kind_perfer_ctx)
                    _this.model.set('pd_term_perfer',result.pd_term_perfer_ctx)
                    _this.riskResult = {
                        answer_id:resp.result.answer_id,
                        low_risk_flag:resp.result.low_risk_flag,
                        ques_invest_num:resp.result.ques_invest_num,
                        risk_accept:resp.result.risk_accept
                    }
                }
            })
            ajax.post('cpliL.1.59', {paper_type: curr_cust.cust_type === 1 ? 2 : 1}).success(function (resp) {
                if(resp.result){
                    resp.result.forEach(function(item){
                        item.adapt_product = _this.getAdaptProduct(item.risk_accept)
                    })
                    let html = art.compile(rowTpl)(resp.result)
                    _this.$el.find('.risk-result_table_body').html(html)
                }
            })
            this.startInterval(5)
        },
        startInterval:function(delay){
            const _this = this;
            this.enableConfirm()
            let interval = setInterval(function(){
                delay--
                _this.delay = delay
                if(delay >0){
                    _this.$el.find('a[name="confirm"]').html('（还剩'+delay+'秒）')
                }else{
                    _this.removeInterval(interval)
                }
            },1000)
            this.once('hidden',function(){_this.removeInterval(interval)})
        },
        removeInterval:function(interval){
            clearInterval(interval)
            this.delay = 0
            this.$el.find('a[name="confirm"]').html('确认')
            this.enableConfirm()
        },
        getAdaptProduct:function(risk_accept){
            switch(risk_accept){
                case 0:
                    return '无'
                case 1:
                    return 'R1'
                case 2:
                    return 'R1,R2'
                case 3:
                    return 'R1,R2,R3'
                case 4:
                    return 'R1,R2,R3,R4'
                case 5:
                    return 'R1,R2,R3,R4,R5'
                default:
                    return 'R1,R2,R3,R4,R5'
            }
        },
        toggleConfirm:function(event){
            this.enableConfirm()
        },
        enableConfirm:function(){
            if(this.$el.find('#risk_result_agree')[0].checked && this.delay == 0){
                this.$el.find('a[name="confirm"]').removeAttr('disabled').removeClass('weui-btn_disabled')
            }else{
                this.$el.find('a[name="confirm"]').addClass('weui-btn_disabled').attr('disabled','disabled')
            }
        }
    })
    return new View()
})